MyDesktop.InputStorageWindow = Ext.extend(Ext.app.Module, {
    id:'inStorage-win',
    init : function(){
        this.launcher = {
            text: '入库管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('inStorage-win');
		
        if(!win){
		
		var store= new Ext.data.JsonStore({
	      root:'',
		  fields:[
    	    {name : 'name',mapping : 'name',type : 'string'}, 
    		{name : 'productCode',mapping : 'productCode',type : 'string'},
    	    {name : 'unit',mapping : 'unit',type : 'string'}, 
    	    {name : 'amount',mapping : 'amount'}, 
    		{name : 'unitPrice',mapping : 'unitPrice'},
    		{name : 'storageId',mapping : 'storageId',type : 'int'},
			{name : 'storageName',mapping : 'storageName',type : 'string'}
		  ]
	    }); 
		
        var buyItem_form = new Ext.FormPanel({
            	border : true,
				region: 'north',
                labelAlign : 'right',
                labelWidth : 60,
				frame : true,
				height: 100,
            	items : [{                   	
                    layout:'hbox',
                    layoutConfig:{align:'middle'},
                    items:[
                       {
		                xtype:'spacer',
                        flex:1
		               },{
                        xtype:'label',
                        text:'入库单',
                         style: {font:'bold 20px arial,serif'}
                       },{
		                xtype:'spacer',
                        flex:1
		               },{
                      	 xtype:'label',
                      	 text : '单据号:',                     	      
                      	 style:{fontSize:'13px'}
                       },{
						 xtype : 'textfield',																				  
					     name : 'docID',							 
						 readOnly :true,
						 style:{background:'#dddddd', width:'120px'}
					   }
                    ]
                  },{
                    html:'<hr color="#34A4E0" />'
                  },{
									layout:'column',
									items:[{
        								columnWidth : .3,
        								layout : 'form',
        								items : [getPartnerCombo()]
								    },{
        								columnWidth : .2,
        								layout : 'form',
        								items : [getEmployeeCombo('经手人')]
								    },{
        								columnWidth : .25,
        								layout : 'form',
        								items : [{xtype : 'datefield',fieldLabel : '购买日期',name : 'buyDate',anchor : '100%',allowBlank : false,format:'Y-m-d'}]
								    }]
							},{// 4 row
									layout:'column',
									items:[{
        								columnWidth : .75,
        								layout : 'form',
        								items : [{xtype : 'textarea',fieldLabel : '备注',name : 'remarks',anchor : '100%',height: 48}]
								    }]
							}]
						});
		

	
	 var cm=new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '',width : 30,dataIndex : 'goodsId',hidden:true}, 
		   {header : '商品名称',width : 130,dataIndex : 'name'}, 
		   {header : '商品编码',width : 130,dataIndex : 'productCode'},
		   {header : '单位',width : 50,dataIndex : 'unit'},
		   {header : '数量', width: 80, dataIndex : 'amount'},
		   {header : "单价", width:100, dataIndex : 'unitPrice', renderer: Ext.util.Format.cnMoney},
		   {header : '金额',width:100,dataIndex : 'totalAmount', groupable: false,
		         renderer:function (v, params, record){
		         	return Ext.util.Format.cnMoney(record.data.amount * record.data.unitPrice);
		         },
		         summaryRenderer: Ext.util.Format.cnMoney
		   },
		   {header:'', dataIndex: 'storageId',hidden:true},
		   {header : '仓库',width : 100,dataIndex : 'storageName'}
	]);
	
	 var itemRecord= Ext.data.Record.create([
	        {name : 'goodsId'}, 
        	{name : 'name'}, 
        	{name : 'productCode'},
        	{name : 'unit'},
        	{name : 'amount'},
        	{name : 'totalAmount'},
			{name : 'storageId'},
			{name : 'storageName'}
    	]);
		
     var product_detailForm =new Ext.FormPanel({
                		border : true,
                		labelAlign : 'right',
                		labelWidth : 60,
						frame : true,
						height: 190,
                		items : [{
									layout:'column',
									items:[{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getGoodsCombo(false)]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'textfield',fieldLabel : '商品编码',name : 'amount',anchor : '100%',disabled : true}]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'textfield',fieldLabel : '单位',name : 'unit',anchor : '100%',disabled : true}]
								    }]
							},{// 2 row
									layout:'column',
									items:[{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'numberfield',fieldLabel : '数量',name : 'amount',anchor : '100%',allowBlank : false, minLength : 1,maxLength : 9,itemId:'addform'}]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [{xtype : 'numberfield',fieldLabel : '进价',name : 'unitPrice',anchor : '100%',allowBlank : false, minLength : 1,maxLength : 9}]
								    },{
        								columnWidth : .33,
        								layout : 'form',
        								items : [getStorageCombo(false)]
								    }]
							}]
						});

       		 
       		 var addOrEditProduct_win = new Ext.Window({
       		 	layout:'fit',
		    	width:630,
		    	closeAction:'hide',
		   		height:200,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[product_detailForm],
				buttonAlign : 'center',
				buttons:[{
                			     	id:'commitButton',
									iconCls : 'save',
									handler:function(){	
									  var form=product_detailForm.getForm();
									  if(!form.isValid()){
	         		                     return;
	      		                       }
									   var values = form.getValues();
	      		                       values.name = form.findField('goodsId').getRawValue();
	      		                       values.storageName = form.findField('storageId').getRawValue();
									   var r=new itemRecord(values);
									   //alert(Ext.encode(values));
                			     	   store.add(r);
									   form.reset();
                			     	}
                			       },{
                			     	 id:'closeButton',
									 text : '关闭',
						             iconCls : 'close',
                			     	 handler:function(){				     		
                			     	    addOrEditProduct_win.hide();
                			     	}
                			      }]
       		 });
     
		  function addProduct(){
            	if(product_detailForm.form.el){
            		product_detailForm.form.el.dom.reset();            	
            	}else{
            		product_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditProduct_win.isAdd = true;
		        addOrEditProduct_win.setTitle("商品入库");
		        addOrEditProduct_win.show();
            }		 
			 
	 var grid_debit = new Ext.grid.EditorGridPanel({
        	title : '入库条目',
        	iconCls : 'icon-grid',
        	collapsible: false,
			border:false,
            region: 'center',
            margins: '5 0 0 0',
			columnLines :true,
			autoScroll : true,
			clicksToEdit: 1,
			enableHdMenu : false,
        	viewConfig:
        	{
        	 forceFit:false,
        	 scrollOffset:15
        	 },
			 tbar:[{
                    text:'增加',
                    tooltip:'',
                    iconCls:'add',
					handler : addProduct
                   },'-',{
                    text:'删除',
                    tooltip:'',
                    iconCls:'remove',
					handler : function(){
			  				var record = grid_debit.getSelectionModel().getSelected();
		       				if (record) {
			     				store.remove(record);
								grid_debit.getView().refresh();
			   				}
			 			}
                   }],
        	autoWidth : true,
        	region : 'center',
        	cm : cm,
        	ds : store,
        	sm : new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
        	enableColumnMove : false,
        	trackMouseOver : false,
        	frame : true,
			clicksToEdit: 1
        });	
		
	
		
      function submitForm(){
			    var form=buyItem_form.getForm();
				var totalRecords =store.getRange();
				var buyItem = [];
				for(var i=0;i<totalRecords.length;i++){
				   
				    buyItem.push(totalRecords[i].data);
				
				}
            	if(!form.isValid()){
	         		 return;
	      		}
				
					Ext.Ajax.request({
	           			url:app.url+'/admin/storage/createBuy.do',
	           			method :"post",
						jsonData :Ext.encode({buy:form.getValues(),buyItems:buyItem}),
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							
							Ext.MessageBox.alert("提示","操作成功!");
							
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
				
		}		
		
		
		
            win = desktop.createWindow({
                id: 'inStorage-win',
                title:'入库',
                width:app.maxWidth,
                height:app.maxHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'border',
                items:[grid_debit,buyItem_form],
                buttonAlign : 'center',
                buttons: [{
                    text:'保存',
					iconCls : 'save',
					handler:submitForm
                },{
                    text: '取消',
					iconCls : 'close',
                    handler: function(){
                      //  addInstall_win.hide();
                    }
                }]    
            });
        }
        win.show();
    }
});